package com.ffcs.trino.interceptor;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义拦截器类
 *
 * @author zhangzhengxing
 */
public class GlobalHandlerInterceptor implements HandlerInterceptor {

  /**
   * 访问控制器方法前执行
   */
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
          throws Exception {
    String key = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkZXB0IjoiMSIsInVzZXJOYW1lIjoienp4IiwiZXhwIjoxNzkxMjQ4NTM4L";
    if (ObjectUtil.isNotEmpty(request.getHeader("Authorization")) && key.equals(request.getHeader("Authorization"))) {
      return true;
    } else {
      throw new RuntimeException("验证错误无法获取数据!");
    }
  }

  /**
   * 访问控制器方法后执行
   */
  @Override
  public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                         ModelAndView modelAndView) throws Exception {
//    System.out.println(new Date() + "--postHandle:" + request.getRequestURL());
  }

  /**
   * postHandle方法执行完成后执行，一般用于释放资源
   */
  @Override
  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
          throws Exception {
    //  System.out.println(new Date() + "--afterCompletion:" + request.getRequestURL());
  }
}